package com.ruoyi.customer.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.ShippingAddress;
import com.ruoyi.customer.service.IShippingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收货地址Controller
 * 
 * @author luogongquan
 * @date 2024-09-10
 */
@RestController
@RequestMapping("/shippingAddress")
public class ShippingAddressController extends BaseController
{
    @Autowired
    private IShippingAddressService shippingAddressService;

    /**
     * 查询收货地址列表
     */
    @RequiresPermissions("customer:shippingAddress:list")
    @GetMapping("/list")
    public TableDataInfo list(ShippingAddress shippingAddress)
    {
        startPage();
        List<ShippingAddress> list = shippingAddressService.selectShippingAddressList(shippingAddress);
        return getDataTable(list);
    }
    /**
     * 查询收货地址列表
     */
    @GetMapping("wx/list")
    public AjaxResult wxList(ShippingAddress shippingAddress)
    {
        shippingAddress.setCustomerId(SecurityUtils.getUserId());
        List<ShippingAddress> list = shippingAddressService.selectShippingAddressList(shippingAddress);
        return success(list);
    }

    /**
     * 导出收货地址列表
     */
    @RequiresPermissions("customer:shippingAddress:export")
    @Log(title = "收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShippingAddress shippingAddress)
    {
        List<ShippingAddress> list = shippingAddressService.selectShippingAddressList(shippingAddress);
        ExcelUtil<ShippingAddress> util = new ExcelUtil<ShippingAddress>(ShippingAddress.class);
        util.exportExcel(response, list, "收货地址数据");
    }

    /**
     * 获取收货地址详细信息
     */
    @RequiresPermissions("customer:shippingAddress:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shippingAddressService.selectShippingAddressById(id));
    }
    /**
     * 获取收货地址详细信息
     */
    @GetMapping(value = "getAddressInfo/{id}")
    @InnerAuth
    public R getAddressInfo(@PathVariable("id") Long id)
    {
        return R.ok(shippingAddressService.selectShippingAddressById(id));
    }

    /**
     * 获取收货地址详细信息
     */
    @GetMapping(value = "wx/{id}")
    public AjaxResult getWxInfo(@PathVariable("id") Long id)
    {
        return success(shippingAddressService.selectShippingAddressById(id));
    }

    /**
     * 新增收货地址
     */
    @RequiresPermissions("customer:shippingAddress:add")
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShippingAddress shippingAddress)
    {
        return toAjax(shippingAddressService.insertShippingAddress(shippingAddress));
    }

    /**
     * 新增收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.INSERT)
    @PostMapping(value = "wx/add")
    public AjaxResult wxAdd(@RequestBody ShippingAddress shippingAddress)
    {
        shippingAddress.setCustomerId(SecurityUtils.getUserId());
        return toAjax(shippingAddressService.insertShippingAddress(shippingAddress));
    }

    /**
     * 修改收货地址
     */
    @RequiresPermissions("customer:shippingAddress:edit")
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShippingAddress shippingAddress)
    {
        return toAjax(shippingAddressService.updateShippingAddress(shippingAddress));
    }
    /**
     * 修改收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.UPDATE)
    @PostMapping(value = "wx/edit")
    public AjaxResult wxEdit(@RequestBody ShippingAddress shippingAddress)
    {
        shippingAddress.setCustomerId(SecurityUtils.getUserId());
        return toAjax(shippingAddressService.updateShippingAddress(shippingAddress));
    }

    /**
     * 删除收货地址
     */
    @RequiresPermissions("customer:shippingAddress:remove")
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shippingAddressService.deleteShippingAddressByIds(ids));
    }

    /**
     * 删除收货地址
     */
    @Log(title = "收货地址", businessType = BusinessType.DELETE)
	@GetMapping("wx/remove/{ids}")
    public AjaxResult wxRemove(@PathVariable Long[] ids)
    {
        return toAjax(shippingAddressService.deleteShippingAddressByIds(ids));
    }

    @GetMapping("wx/getDefaultAddress")
    public AjaxResult getDefaultAddress(){
        return success(shippingAddressService.getDefaultAddress());
    }
}
